﻿package mortal.game.view.petNew.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mount.newMountModuel.*;
    import mortal.game.view.skill.*;
    import mortal.game.view.skill.panel.render.*;
    import mortal.mvc.core.*;

    public class NewPetSkillPanel extends GSprite
    {
        private var _talentSkillBg1:GBitmap;
        private var _talentSkillBg:GBitmap;
        private var _talentSkill:SkillItem;
        private var _dicSkillItem:Dictionary;

        public function NewPetSkillPanel()
        {
            this._dicSkillItem = new Dictionary();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._talentSkillBg1 = UIFactory.gBitmap("", 20, 469, this);
            this._talentSkillBg = UIFactory.gBitmap(ImagesConst.petTalentSkillBg, 32, 484, this);
            this._talentSkill = UIFactory.getUICompoment(SkillItem, 49, 501, this);
            this._talentSkill.setSize(42, 42);
            this._talentSkill.setBg(ImagesConst.PetTalentSkill);
            this._talentSkillBg1.bitmapData = GlobalClass.getBitmapData(ImagesConst.PetInfoLeftTalentBg);
            this.createSkillList();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._talentSkillBg = null;
            this._talentSkill = null;
            this._talentSkillBg1 = null;
            this._dicSkillItem = new Dictionary();
            super.disposeImpl(param1);
            return;
        }// end function

        private function createSkillList() : void
        {
            var _loc_1:OtherSkillItem = null;
            var _loc_3:TMountConfig = null;
            var _loc_4:TSkill = null;
            var _loc_5:SkillInfo = null;
            var _loc_6:int = 0;
            var _loc_2:* = MountConfig.instance.getFashionsByType(EAdvanceComponentType._EAdvanceComponentTypePet);
            _loc_2.sortOn("code", Array.NUMERIC);
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_8];
                if (_loc_3.skillList)
                {
                    _loc_7++;
                    _loc_5 = new SkillInfo();
                    _loc_4 = SkillConfig.instance.getInfoById(int(_loc_3.skillList));
                    _loc_5.tSkill = _loc_4;
                    _loc_1 = UICompomentPool.getUICompoment(OtherSkillItem);
                    _loc_1.setBg(ImagesConst.EquipItemBg, 7, 7);
                    _loc_1.setSize(48, 48);
                    _loc_1.isDragAble = false;
                    _loc_1.isDragAble = false;
                    _loc_1.x = 65 + 48 * _loc_7;
                    _loc_1.y = 512;
                    _loc_1.doubleClickEnabled = true;
                    _loc_1.skillInfo = _loc_5;
                    this.pushUIToDisposeVec(_loc_1);
                    this._dicSkillItem[_loc_4.posType] = _loc_1;
                    this.addChild(_loc_1);
                    _loc_6++;
                }
                _loc_8++;
            }
            return;
        }// end function

        public function updateSkillGrid() : void
        {
            var _loc_3:TSkill = null;
            var _loc_4:OtherSkillItem = null;
            var _loc_5:TMountConfig = null;
            if (Cache.instance.newPet.pet == null)
            {
                return;
            }
            var _loc_1:* = Cache.instance.newPet.pet.code;
            var _loc_2:* = MountConfig.instance.getFashionsByType(EAdvanceComponentType._EAdvanceComponentTypePet);
            for each (_loc_4 in this._dicSkillItem)
            {
                
                for each (_loc_5 in _loc_2)
                {
                    
                    if (_loc_5.skillList)
                    {
                        _loc_3 = SkillConfig.instance.getInfoById(int(_loc_5.skillList));
                        if (_loc_3.posType == _loc_4.skillInfo.tSkill.posType)
                        {
                            if (_loc_1 >= _loc_5.code)
                            {
                                _loc_4.isShowLock = false;
                            }
                            else
                            {
                                _loc_4.isShowLock = true;
                            }
                            break;
                        }
                    }
                }
            }
            this.updateSkill();
            return;
        }// end function

        public function updateSkill() : void
        {
            var _loc_7:SkillInfo = null;
            var _loc_9:OtherSkillItem = null;
            var _loc_1:* = Cache.instance.newPet.getTalentSkillList;
            var _loc_2:* = _loc_1.length > 0 ? (_loc_1[0]) : (null);
            this._talentSkill.setSkillInfo(_loc_2);
            var _loc_3:Array = [];
            var _loc_4:* = Cache.instance.newPet.pet;
            var _loc_5:int = 0;
            _loc_5 = 0;
            while (_loc_5 < 8)
            {
                
                if (_loc_4.code >= (_loc_5 + 1))
                {
                    _loc_2 = Cache.instance.newPet.getPetSkill(_loc_5 + PetConst.PASSIVE_SKILL_START_POS);
                    if (_loc_2)
                    {
                        _loc_3.push(_loc_2);
                    }
                }
                _loc_5++;
            }
            var _loc_6:* = new Dictionary();
            _loc_5 = 0;
            while (_loc_5 < _loc_3.length)
            {
                
                _loc_2 = _loc_3[_loc_5];
                _loc_7 = _loc_6[_loc_2.tSkill.posType] as SkillInfo;
                if (_loc_7 == null || _loc_7 && _loc_2.tSkill.skillLevel > _loc_7.tSkill.skillLevel)
                {
                    _loc_6[_loc_2.tSkill.posType] = _loc_2;
                }
                _loc_5++;
            }
            var _loc_8:int = 0;
            for each (_loc_9 in this._dicSkillItem)
            {
                
                _loc_2 = _loc_6[_loc_9.skillInfo.tSkill.posType];
                if (_loc_2)
                {
                    _loc_9.skillInfo = _loc_2;
                    _loc_9.hasLearnSkill = true;
                    continue;
                }
                _loc_9.hasLearnSkill = false;
            }
            this.updateSkillbook();
            return;
        }// end function

        public function updateSkillbook() : void
        {
            var _loc_2:TSkill = null;
            var _loc_3:OtherSkillItem = null;
            var _loc_4:ItemData = null;
            var _loc_1:* = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, EAdvanceType._EAdvanceTypePet);
            for each (_loc_3 in this._dicSkillItem)
            {
                
                _loc_3.isShowUpgradeBtn = false;
                _loc_3.setState(OtherSkillItem.stateType_no);
                if (!_loc_3.isLocking)
                {
                    for each (_loc_4 in _loc_1)
                    {
                        
                        _loc_2 = SkillConfig.instance.getInfoById(_loc_4.itemInfo.effect);
                        if (_loc_3.skillInfo.tSkill.posType == _loc_2.posType)
                        {
                            if (_loc_3.hasLearnSkill)
                            {
                                if (_loc_2.skillLevel - _loc_3.skillInfo.tSkill.skillLevel == 1)
                                {
                                    _loc_3.isShowUpgradeBtn = true;
                                    _loc_3.setState(OtherSkillItem.stateType_canAdvance);
                                    _loc_3.upgradeParam = _loc_4;
                                    _loc_3.callBack = this.upgradeSkill;
                                    _loc_3.setBtnPosition(28, 2);
                                    break;
                                }
                                else
                                {
                                    _loc_3.isShowUpgradeBtn = false;
                                    _loc_3.setState(OtherSkillItem.stateType_no);
                                }
                            }
                            else if (_loc_2.skillLevel == 1)
                            {
                                _loc_3.isShowUpgradeBtn = true;
                                _loc_3.setState(OtherSkillItem.stateType_canLearn);
                                _loc_3.upgradeParam = _loc_4;
                                _loc_3.callBack = this.upgradeSkill;
                                _loc_3.setBtnPosition(28, 2);
                                break;
                            }
                            else
                            {
                                _loc_3.isShowUpgradeBtn = false;
                                _loc_3.setState(OtherSkillItem.stateType_no);
                            }
                            continue;
                        }
                        _loc_3.isShowUpgradeBtn = false;
                        _loc_3.setState(OtherSkillItem.stateType_no);
                    }
                }
            }
            return;
        }// end function

        private function upgradeSkill(param1:ItemData) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetNewUseSkillBook, param1));
            return;
        }// end function

    }
}
